#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
'''
import aiohttp
import asyncio
import requests
import time


def timing(f):
    """function timing wrapper"""

    def wrapper(*arg, **kw):
        t1 = time.time()
        ret = f(*arg, **kw)
        t2 = time.time()
        print('took: %2.4f sec: func:%r args:[%r, %r] ' % \
              (t2 - t1, f.__name__, arg, kw))
        return ret

    return wrapper


@timing
def using_aiohttp(urls):
    async def process_one_url(url):
        print("aiohttp send request")
        r = await aiohttp.get(url)
        print("aiohttp get response %s" % r.status)
        r.close()

    loop = asyncio.get_event_loop()
    tasks = asyncio.gather(*[process_one_url(url) for url in urls])
    loop.run_until_complete(tasks)


@timing
def using_requests(urls):
    for url in urls:
        print("requests send request")
        r = requests.get(url)
        print("requests get response %s" % r.status_code)


def main():
    urls = ["http://www.apicloud.com/getAllUnpack?appId=A6962696696314&startNum=0&num=10",
           "http://www.apicloud.com/mam/getSubVersionListByAppid",
           "http://www.apicloud.com/mam/addSubVersion",
           "http://www.apicloud.com/uzampush_insert",
           "http://www.apicloud.com/getV2AppStatisticDataByAppIdAndDate",
           "http://www.apicloud.com/getCer?appId=A6962696696314",
           "http://www.apicloud.com/checkDevUser",
           "http://www.apicloud.com/bbsLogin.html",
           "http://www.apicloud.com/bingModule",
           "http://www.apicloud.com/getPKGState",
           ]
    using_aiohttp(urls)
    using_requests(urls)


if __name__ == "__main__":
    main()
